//*********************************************
//ADRIAN SALAZAR && JUSTIN MICHALE SCHWARTZBECK
//HOMEWORK 2
//FEB. 23, 2012
//
//Client Buffer.h file contains the header files
//to the clientbuffer.cpp class
//*********************************************


#include "packet.h"
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <errno.h>
#include <strings.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <list>

// IO Thread declaration
void * cli_io_thread_func(void * arg);


class ClientBuffer {
public:

	// Connection data
	struct sockaddr_in client;
	int sock;
	int port;
	int id;
	
	// Concurrency data
	pthread_t io_thread;
	bool running;
	pthread_cond_t data_sent;
	pthread_cond_t ack_received;
	pthread_mutex_t	in_lock;
	pthread_mutex_t out_lock;
	pthread_mutex_t list_lock;
	pthread_mutex_t timer_lock;

	//time data	
	double epoch;
	int numepochs;
	double drop_rate;
	time_t last_io_event;

	//packet information
	std::list<Packet> out_buf;
	std::list<Packet> in_buf;
	std::list<Packet> ack_buf;
	
	uint32_t conn_id;
	uint32_t seq_num;
	uint32_t ack_seq;
	
	// State
	int state;
	char * errmsg;

	ClientBuffer(const char * ip, int in_port, double epoch_len, int e_count, double d_rate);
	
	int read(uint8_t * data);
	int write(uint8_t * data, uint32_t len);
	bool is_running(); 
	void finish();
};

